<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="用户名" prop="username">
          <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
          <el-form-item label="密码" prop="password">
          <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
          <el-form-item label="iot_user_id" prop="iotUserId">
          <el-input v-model="dataForm.iotUserId" placeholder="iot_user_id"></el-input>
      </el-form-item>
          <el-form-item label="姓名" prop="realName">
          <el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
      </el-form-item>
          <el-form-item label="头像" prop="headUrl">
          <el-input v-model="dataForm.headUrl" placeholder="头像"></el-input>
      </el-form-item>
          <el-form-item label="性别   0：男   1：女    2：保密" prop="gender">
          <el-input v-model="dataForm.gender" placeholder="性别   0：男   1：女    2：保密"></el-input>
      </el-form-item>
          <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
          <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
          <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="dataForm.deptId" placeholder="部门ID"></el-input>
      </el-form-item>
          <el-form-item label="超级管理员   0：否   1：是" prop="superAdmin">
          <el-input v-model="dataForm.superAdmin" placeholder="超级管理员   0：否   1：是"></el-input>
      </el-form-item>
          <el-form-item label="状态  0：停用   1：正常" prop="status">
          <el-input v-model="dataForm.status" placeholder="状态  0：停用   1：正常"></el-input>
      </el-form-item>
              <el-form-item label="更新者" prop="updater">
          <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item>
          <el-form-item label="租户ID" prop="tenantId">
          <el-input v-model="dataForm.tenantId" placeholder="租户ID"></el-input>
      </el-form-item>
          <el-form-item label="工号" prop="eeNumber">
          <el-input v-model="dataForm.eeNumber" placeholder="工号"></el-input>
      </el-form-item>
          <el-form-item label="民族" prop="nation">
          <el-input v-model="dataForm.nation" placeholder="民族"></el-input>
      </el-form-item>
          <el-form-item label="政治面貌" prop="political">
          <el-input v-model="dataForm.political" placeholder="政治面貌"></el-input>
      </el-form-item>
          <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="dataForm.idNumber" placeholder="身份证号"></el-input>
      </el-form-item>
          <el-form-item label="现住址" prop="address">
          <el-input v-model="dataForm.address" placeholder="现住址"></el-input>
      </el-form-item>
          <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="dataForm.nativePlace" placeholder="籍贯"></el-input>
      </el-form-item>
          <el-form-item label="身高" prop="height">
          <el-input v-model="dataForm.height" placeholder="身高"></el-input>
      </el-form-item>
          <el-form-item label="体重" prop="weight">
          <el-input v-model="dataForm.weight" placeholder="体重"></el-input>
      </el-form-item>
          <el-form-item label="血型" prop="blood">
          <el-input v-model="dataForm.blood" placeholder="血型"></el-input>
      </el-form-item>
          <el-form-item label="健康状况" prop="healthy">
          <el-input v-model="dataForm.healthy" placeholder="健康状况"></el-input>
      </el-form-item>
          <el-form-item label="学历" prop="education">
          <el-input v-model="dataForm.education" placeholder="学历"></el-input>
      </el-form-item>
          <el-form-item label="毕业院校" prop="graduate">
          <el-input v-model="dataForm.graduate" placeholder="毕业院校"></el-input>
      </el-form-item>
          <el-form-item label="专业" prop="major">
          <el-input v-model="dataForm.major" placeholder="专业"></el-input>
      </el-form-item>
          <el-form-item label="开户行" prop="bank">
          <el-input v-model="dataForm.bank" placeholder="开户行"></el-input>
      </el-form-item>
          <el-form-item label="银行卡号" prop="bankCard">
          <el-input v-model="dataForm.bankCard" placeholder="银行卡号"></el-input>
      </el-form-item>
          <el-form-item label="入职时间" prop="entryTime">
          <el-input v-model="dataForm.entryTime" placeholder="入职时间"></el-input>
      </el-form-item>
          <el-form-item label="职务" prop="post">
          <el-input v-model="dataForm.post" placeholder="职务"></el-input>
      </el-form-item>
          <el-form-item label="部门" prop="department">
          <el-input v-model="dataForm.department" placeholder="部门"></el-input>
      </el-form-item>
          <el-form-item label="自我介绍" prop="description">
          <el-input v-model="dataForm.description" placeholder="自我介绍"></el-input>
      </el-form-item>
          <el-form-item label="转正时间" prop="confirmDate">
          <el-input v-model="dataForm.confirmDate" placeholder="转正时间"></el-input>
      </el-form-item>
          <el-form-item label="在职状态" prop="jobStatus">
          <el-input v-model="dataForm.jobStatus" placeholder="在职状态"></el-input>
      </el-form-item>
          <el-form-item label="离职时间" prop="leftDate">
          <el-input v-model="dataForm.leftDate" placeholder="离职时间"></el-input>
      </el-form-item>
          <el-form-item label="负责区域" prop="areas">
          <el-input v-model="dataForm.areas" placeholder="负责区域"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        username: '',
        password: '',
        iotUserId: '',
        realName: '',
        headUrl: '',
        gender: '',
        email: '',
        mobile: '',
        deptId: '',
        superAdmin: '',
        status: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        tenantId: '',
        eeNumber: '',
        nation: '',
        political: '',
        idNumber: '',
        address: '',
        nativePlace: '',
        height: '',
        weight: '',
        blood: '',
        healthy: '',
        education: '',
        graduate: '',
        major: '',
        bank: '',
        bankCard: '',
        entryTime: '',
        post: '',
        department: '',
        description: '',
        confirmDate: '',
        jobStatus: '',
        leftDate: '',
        areas: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        iotUserId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        realName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        headUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        gender: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        superAdmin: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tenantId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        eeNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        political: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        idNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        address: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        nativePlace: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        height: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        weight: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        blood: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        healthy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        education: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        graduate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        major: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bank: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bankCard: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        entryTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        post: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        department: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        description: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        confirmDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        jobStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        leftDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        areas: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/hr/sysuser/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/hr/sysuser/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
